﻿using System.Collections.Generic;
using System.Threading.Tasks;
using WeiboWPSdk.Json;
using WeiboWPSdk.ResponseEntity;

namespace WeiboWPSdk.Api {
    public class CommentApi : ApiBase {
        public CommentApi(ClientHttpHelper httpHelper)
            : base(httpHelper) {
        }

        #region 读取接口
        /// <summary>
        /// 根据微博ID返回某条微博的评论列表
        /// http://open.weibo.com/wiki/2/comments/show
        /// </summary>
        /// <param name="id">需要查询的微博ID。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterByAuthor">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <returns></returns>
        public async Task<CommentCollection> ShowAsync(long id, long sinceId = 0, long maxId = 0, int count = 50, int page = 1, int filterByAuthor = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("comments/show", new WeiboParameter("id", id), new WeiboParameter("since_id", sinceId),
                new WeiboParameter("max_id", maxId), new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("filter_by_author", filterByAuthor));
            return JsonConvert.DeserializeObject<CommentCollection>(json);
        }
        /// <summary>
        /// 获取当前登录用户所发出的评论列表
        /// http://open.weibo.com/wiki/2/comments/by_me
        /// </summary>
        /// <param name="id">需要查询的微博ID。</param>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterBySource">来源筛选类型，0：全部、1：来自微博的评论、2：来自微群的评论，默认为0。</param>
        /// <returns></returns>
        public async Task<CommentCollection> ByMeAsync(long sinceId = 0, long maxId = 0, int count = 50, int page = 1, int filterBySource = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("comments/by_me", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("filter_by_source", filterBySource));
            return JsonConvert.DeserializeObject<CommentCollection>(json);
        }
        /// <summary>
        /// 获取当前登录用户所接收到的评论列表
        /// http://open.weibo.com/wiki/2/comments/to_me
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterByAuthor">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <param name="filterBySource">来源筛选类型，0：全部、1：来自微博的评论、2：来自微群的评论，默认为0。</param>
        /// <returns></returns>
        public async Task<CommentCollection> ToMeAsync(long sinceId = 0, long maxId = 0, int count = 50, int page = 1, int filterByAuthor = 0, int filterBySource = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("comments/to_me", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("filter_by_author", filterByAuthor), new WeiboParameter("filter_by_source", filterBySource));
            return JsonConvert.DeserializeObject<CommentCollection>(json);
        }
        /// <summary>
        /// 获取当前登录用户的最新评论包括接收到的与发出的
        /// http://open.weibo.com/wiki/2/comments/timeline
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="trimUser">返回值中user字段开关，0：返回完整user字段、1：user字段仅返回user_id，默认为0。</param>
        /// <returns></returns>
        public async Task<CommentCollection> TimelineAsync(long sinceId = 0, long maxId = 0, int count = 50, int page = 1, bool trimUser = false) {
            var json = await HttpHelper.GetBasicCommandAsync("comments/timeline", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("trim_user", trimUser));
            return JsonConvert.DeserializeObject<CommentCollection>(json);
        }
        /// <summary>
        /// 获取最新的提到当前登录用户的评论，即@我的评论
        /// http://open.weibo.com/wiki/2/comments/mentions
        /// </summary>
        /// <param name="sinceId">若指定此参数，则返回ID比since_id大的评论（即比since_id时间晚的评论），默认为0。</param>
        /// <param name="maxId">若指定此参数，则返回ID小于或等于max_id的评论，默认为0。</param>
        /// <param name="count">单页返回的记录条数，默认为50。</param>
        /// <param name="page">返回结果的页码，默认为1。</param>
        /// <param name="filterByAuthor">作者筛选类型，0：全部、1：我关注的人、2：陌生人，默认为0。</param>
        /// <param name="filterBySource">来源筛选类型，0：全部、1：来自微博的评论、2：来自微群的评论，默认为0。</param>
        /// <returns></returns>
        public async Task<CommentCollection> MentionsAsync(long sinceId = 0, long maxId = 0, int count = 50, int page = 1, int filterByAuthor = 0, int filterBySource = 0) {
            var json = await HttpHelper.GetBasicCommandAsync("comments/mentions", new WeiboParameter("since_id", sinceId), new WeiboParameter("max_id", maxId),
                new WeiboParameter("count", count), new WeiboParameter("page", page), new WeiboParameter("filter_by_author", filterByAuthor), new WeiboParameter("filter_by_source", filterBySource));
            return JsonConvert.DeserializeObject<CommentCollection>(json);
        }
        /// <summary>
        /// 根据评论ID批量返回评论信息
        /// http://open.weibo.com/wiki/2/comments/show_batch
        /// </summary>
        /// <param name="cids">需要查询的批量评论ID，用半角逗号分隔，最大50。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Comment>> ShowBatchAsync(params long[] cids) {
            var json = await HttpHelper.GetBasicCommandAsync("comments/show_batch", new WeiboParameter("cids", string.Join(",", cids)));
            return JsonConvert.DeserializeObject<IEnumerable<Comment>>(json);
        }
        #endregion
        #region 写入接口
        /// <summary>
        /// 对一条微博进行评论
        /// http://open.weibo.com/wiki/2/comments/create
        /// </summary>
        /// <param name="comment">评论内容，必须做URLencode，内容不超过140个汉字。</param>
        /// <param name="id">需要评论的微博ID。</param>
        /// <param name="commentOri">当评论转发微博时，是否评论给原微博，0：否、1：是，默认为0。</param>
        /// <returns></returns>
        public async Task<Comment> CreateAsync(string comment, long id, bool commentOri = false) {
            var json = await HttpHelper.PostBasicCommandAsync("comments/create", new WeiboParameter("comment", comment),
                new WeiboParameter("id", id), new WeiboParameter("comment_ori", commentOri));
            return JsonConvert.DeserializeObject<Comment>(json);
        }
        /// <summary>
        /// 删除一条评论
        /// http://open.weibo.com/wiki/2/comments/destroy
        /// </summary>
        /// <param name="cid">要删除的评论ID，只能删除登录用户自己发布的评论。</param>
        /// <returns></returns>
        public async Task<Comment> DestroyAsync(long cid) {
            var json = await HttpHelper.PostBasicCommandAsync("comments/destroy", new WeiboParameter("cid", cid));
            return JsonConvert.DeserializeObject<Comment>(json);
        }
        /// <summary>
        /// 根据评论ID批量删除评论
        /// http://open.weibo.com/wiki/2/comments/destroy_batch
        /// </summary>
        /// <param name="cids">需要删除的评论ID，用半角逗号隔开，最多20个。</param>
        /// <returns></returns>
        public async Task<IEnumerable<Comment>> DestroyBatchAsync(params long[] cids) {
            var json = await HttpHelper.PostBasicCommandAsync("comments/destroy_batch", new WeiboParameter("cids", string.Join(",", cids)));
            return JsonConvert.DeserializeObject<IEnumerable<Comment>>(json);
        }
        /// <summary>
        /// 回复一条评论
        /// http://open.weibo.com/wiki/2/comments/reply
        /// </summary>
        /// <param name="cid">需要回复的评论ID。</param>
        /// <param name="id">需要评论的微博ID。</param>
        /// <param name="comment">回复评论内容，必须做URLencode，内容不超过140个汉字。</param>
        /// <param name="withoutMention">回复中是否自动加入“回复@用户名”，0：是、1：否，默认为0。</param>
        /// <param name="commentOri">当评论转发微博时，是否评论给原微博，0：否、1：是，默认为0。</param>
        /// <returns></returns>
        public async Task<Comment> ReplyAsync(long cid, long id, string comment, bool withoutMention = false, bool commentOri = false) {
            var json = await HttpHelper.PostBasicCommandAsync("comments/reply", new WeiboParameter("cid", cid), new WeiboParameter("id", id), new WeiboParameter("comment", comment),
                new WeiboParameter("without_mention", withoutMention), new WeiboParameter("comment_ori", commentOri));
            return JsonConvert.DeserializeObject<Comment>(json);
        }
        #endregion
    }
}
